﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Zatify.Domain.Data;
using Zatify.WebUI.Models;
using Zatify.Domain.Abstract;
using Zatify.WebUI.Infrastructure.Absrtact;
using Zatify.WebUI.Infrastructure.Filters;
using Zatify.WebUI.Infrastructure.Concrete;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;

namespace Zatify.WebUI.Infrastructure
{
    public enum AdminModules
    {
        Account,
        Users,
        SystemUsers,
        Rates,
        Defaults,
        Categories,
        MessageCenter,
        Business,
        Reports
    }

    public abstract class AdminBaseController : Controller
    {
        protected abstract AdminModules AdminModule { get; }

        protected virtual new ZatifyPrincipal User
        {
            get { return HttpContext.User as ZatifyPrincipal; }
        }

        public AdminBaseController()
        {
            ViewData["Current_Module"] = new ModuleViewModel()
            {
                Module = AdminModule,
                ModuleTitle = GetCurrentModuleTitle(AdminModule)
            };
        }

        private string GetCurrentModuleTitle(AdminModules module)
        {
            switch (module)
            {
                case AdminModules.Account:
                    return "Account";
                case AdminModules.Users:
                    return "Users";
                case AdminModules.SystemUsers:
                    return "System Users";
                case AdminModules.Rates:
                    return "Rates";
                case AdminModules.Defaults:
                    return "Defaults";
                case AdminModules.Categories:
                    return "Categories";
                case AdminModules.MessageCenter:
                    return "Message Center";
                case AdminModules.Business:
                    return "Business";
                case AdminModules.Reports:
                    return "Reports";
                default:
                    return String.Empty;
            }
        }

        public ActionResult UploadTempImageAndTumb(HttpPostedFileBase datafile, string userName)
        {
            //System.Threading.Thread.Sleep(3000);
            using (var image = Image.FromStream(datafile.InputStream, true, true)) /* Creates Image from specified data stream */
            {
                using (var thumb = image.GetThumbnailImage(151, 151, () => false, IntPtr.Zero))
                {
                    /* Returns array of image encoder objects built into GDI+ */
                    var jpgInfo = ImageCodecInfo.GetImageEncoders().Where(codecInfo => codecInfo.MimeType == datafile.ContentType).First();

                    using (var encParams = new EncoderParameters(1))
                    {
                        var fileName = datafile.FileName;
                        if (fileName.Contains("\\"))
                            fileName = fileName.Remove(0, (fileName.LastIndexOf("\\") + 1));

                        var tempFolder = "~/Content/uploads/temp/promotions/thumbnails/";
                        var tempFileName = userName + "_" + fileName;
                        var appDataThumbnailPath = Server.MapPath(tempFolder);

                        if (!Directory.Exists(appDataThumbnailPath))
                        {
                            Directory.CreateDirectory(appDataThumbnailPath);
                        }

                        var outputPath = Path.Combine(appDataThumbnailPath, tempFileName);
                        long quality = 100;

                        encParams.Param[0] = new EncoderParameter(Encoder.Quality, quality);
                        thumb.Save(outputPath, jpgInfo, encParams);

                        return Content(Url.Content(tempFolder + tempFileName));
                    }
                }
            }
        }
    }
}